using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Appcontext.CommandHandler
{
	public class GetDepartmentByIdQueryHandler : IRequestHandler<GetDepartmentByIdQuery, DepartmentDto>
	{
		private readonly IBaseRepository<Department> _repository;

		public GetDepartmentByIdQueryHandler(IBaseRepository<Department> repository)
		{
			_repository = repository;
		}

		public async Task<DepartmentDto> Handle(GetDepartmentByIdQuery request, CancellationToken cancellationToken)
		{
			var entity = await _repository.GetModel(x => x.DepartmentId == request.DepartmentId && !x.IsDeleted);
			if (entity == null) throw new Exception("科室不存在");
			return new DepartmentDto
			{
				DepartmentId = entity.DepartmentId,
				Name = entity.Name,
				Address = entity.Address,
				RegistrationFee = entity.RegistrationFee,
				ParentDepartmentId = entity.ParentDepartmentId,
				IsKeyDepartment = entity.IsKeyDepartment,
				IsEmergencyAvailable = entity.IsEmergencyAvailable,
				BedCount = entity.BedCount,
				CreatedAt = entity.CreatedAt,
				UpdatedAt = entity.UpdatedAt
			};
		}
	}
}
